package com.atguigu.auth.controller;

import com.atguigu.auth.service.SysRoleService;
import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysRole;
import com.atguigu.vo.system.AssignRoleVo;
import com.atguigu.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * className: SysRoleController
 * description:
 * date: 2023/4/11 0011-22:35
 * <p>
 * project: guigu-oa-parent
 * package: com.atguigu.auth.controller
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    /**
     * 注入 service
     */
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 查询所有角色和当前用户所属角色
     *
     * @param userId {@link Long}
     * @return {@link Result<Map<String, Object>>}
     */
    @ApiOperation("获取角色")
    @GetMapping("/toAssign/{userId}")
    public Result<Map<String, Object>> toAssign(@PathVariable Long userId) {
        Map<String, Object> map = sysRoleService.findRoleDataByUserId(userId);
        return Result.ok(map);
    }

    /**
     * 为用户分配角色
     *
     * @param assignRoleVo {@link AssignRoleVo}
     * @return {@link Result<Null>}
     */
    @ApiOperation("为用户分配角色")
    @PostMapping("/doAssign")
    public Result<Null> doAssign(@RequestBody AssignRoleVo assignRoleVo) {
        sysRoleService.doAssign(assignRoleVo);
        return Result.ok();
    }

    /**
     * 统一返回数据结果
     *
     * @return {@link Result<List<SysRole>>}
     */
    @ApiOperation("查询所有角色")
    @GetMapping("findAll")
    public Result<List<SysRole>> findAll() {
        // 调用 service 的方法
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }

    /**
     * 分页查询
     *
     * @param page           {@link Long} 当前页
     * @param limit          {@link Long} 每页显示记录数
     * @param sysRoleQueryVo {@link SysRoleQueryVo} 角色查询条件视图对象
     * @return {@link Result<IPage<SysRole>>} 角色分页查询结果
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result<IPage<SysRole>> pageQueryRole(@PathVariable Long page,
                                                @PathVariable Long limit,
                                                SysRoleQueryVo sysRoleQueryVo) {
        // 1.创建 page 对象，传递分页相关参数
        Page<SysRole> pageParam = new Page<>(page, limit);
        // 2.封装条件，判断条件是否为空，不为空进行封装
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if (!StringUtils.isEmpty(roleName)) {
            // 封装
            wrapper.like(SysRole::getRoleName, roleName);
        }
        // 3.调用方法实现
        IPage<SysRole> pageModel = sysRoleService.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }

    /**
     * 添加角色
     *
     * @param role {@link SysRole} 角色
     * @return {@link Result<Null>}
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加角色")
    @PostMapping("save")
    public Result<Null> save(@RequestBody SysRole role) {
        // 调用 service
        boolean isSuccess = sysRoleService.save(role);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 修改角色-根据id查询
     *
     * @param id {@link Long} id
     * @return {@link Result<SysRole>}
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询")
    @GetMapping("get/{id}")
    public Result<SysRole> get(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);
    }

    /**
     * 修改角色-最终修改
     *
     * @param role {@link SysRole} 角色
     * @return {@link Result<Null>}
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("修改角色")
    @PutMapping("update")
    public Result<Null> update(@RequestBody SysRole role) {
        // 调用 service 方法
        boolean isSuccess = sysRoleService.updateById(role);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 根据 id 删除
     *
     * @param id {@link Long} id
     * @return {@link Result<Null>}
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据id删除")
    @DeleteMapping("remove/{id}")
    public Result<Null> remove(@PathVariable Long id) {
        boolean isSuccess = sysRoleService.removeById(id);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 批量删除
     * 前端数组 [1,2,3]
     *
     * @param idList {@link List<Long>} list
     * @return {@link Result<Null>}
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result<Null> remove(@RequestBody List<Long> idList) {
        boolean isSuccess = sysRoleService.removeByIds(idList);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }
}
